package com.github.aloxc.plugin.restplus.listener;

import com.github.aloxc.plugin.restplus.i18n.I18Bundle;
import com.github.aloxc.plugin.restplus.component.TextPane;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.ui.popup.Balloon;
import com.intellij.openapi.ui.popup.JBPopupFactory;

import javax.swing.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import static com.github.aloxc.plugin.restplus.popup.PopupBalloon.NORMAL;

/**
 *
 * @author leero
 */
public class TextPaneKeyAdapter extends KeyAdapter implements MouseListener {
    private final TextPane jTextArea;
    private boolean javascript;

    public TextPaneKeyAdapter(TextPane jTextArea,boolean javascript) {
        this.jTextArea = jTextArea;
        this.javascript = javascript;
    }

    @Override
    public void keyPressed(KeyEvent event) {
        super.keyPressed(event);
        // 当F5组合键按下时响应
        boolean f5 = event.getKeyCode() == KeyEvent.VK_F5;
        if (f5) {
            jTextArea.format();
        }

        if(javascript){
            boolean f6 = event.getKeyCode() == KeyEvent.VK_F6;
            if (f6) {
                showPopupBalloon((TextPane)event.getSource());
            }
        }
    }
    private void showPopupBalloon(final JComponent component) {
        ApplicationManager.getApplication().invokeLater(new Runnable() {
            @Override
            public void run() {
                JBPopupFactory factory = JBPopupFactory.getInstance();
                factory.createHtmlTextBalloonBuilder(I18Bundle.getMessage("com.github.aloxc.plugin.restplus.test.example.text"), null, NORMAL, null)
                        .setFadeoutTime(50000)
                        .createBalloon()
                        .show(factory.guessBestPopupLocation(component), Balloon.Position.above);
            }
        });
    }
    @Override
    public void mouseClicked(MouseEvent e) {

    }

    @Override
    public void mousePressed(MouseEvent e) {
        if(e.getButton() == MouseEvent.BUTTON3){
            JPopupMenu popupMenu = new JPopupMenu();
            JMenuItem item = new JMenuItem(I18Bundle.getMessage("com.github.aloxc.plugin.restplus.textpane.format.text"));
            popupMenu.add(item);
            item.addActionListener(ex->{
                ((TextPane)e.getSource()).format();
            });
            if(javascript){
                JMenuItem avo = new JMenuItem(I18Bundle.getMessage("com.github.aloxc.plugin.restplus.textpane.availabeobject.text"));
                popupMenu.add(avo);
                avo.addActionListener(ex->{
                    showPopupBalloon((TextPane)e.getSource());
                });
            }
            popupMenu.show(((TextPane)e.getSource()),e.getX(),e.getY());
        }
    }

    @Override
    public void mouseReleased(MouseEvent e) {

    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }

    @Override
    public void mouseExited(MouseEvent e) {

    }
}